﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;


namespace IHISCETeim.DAL
{
    /// <summary>
    /// 3.应收账款帐龄明细表(FTradeAgeDetail)
    /// </summary>
    public class FTradeAgeDetailDAL
    {
        /// <summary>
        /// 添加帐龄明细记录,注意,一般不直接使用
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Add(FTradeAgeDetailInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeAgeDetail_Add", obj.FTradeSalesId, obj.ShowIndex, obj.TongJiName, obj.ShowTitle, obj.JinE, obj.DaoQiRi, obj.SuspendedMark, obj.Remark, obj.RealName,obj.YearRate,obj.TanQeChenBen,obj.YuanQiDay);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }

        public static int Adds(List<FTradeAgeDetailInfo> objs)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            using (DbConnection conn = DB.CreateConnection())
            {
                //打开连接
                conn.Open();
                //创建事务
                DbTransaction trans = conn.BeginTransaction();
                try
                {
                    foreach(FTradeAgeDetailInfo obj in objs)
                    { 
                        DbCommand dbCommand = DB.GetStoredProcCommand("FTradeAgeDetail_Add", obj.FTradeSalesId, obj.ShowIndex, obj.TongJiName, obj.ShowTitle, obj.JinE, obj.DaoQiRi, obj.SuspendedMark, obj.Remark, obj.RealName,obj.YearRate,obj.TanQeChenBen,obj.YuanQiDay);
                        int id=Convert.ToInt32(DB.ExecuteScalar(dbCommand, trans));
                        if(id<0)
                        {
                            throw new Exception("输入值有问题.");
                        }
                    }
                    trans.Commit();
                    return 1;
                }
                catch (Exception exc)
                {
                    //发生异常，事务回滚
                    trans.Rollback();
                    return 0;
                }
                finally
                {
                    //关闭连接
                    conn.Close();
                }
            }
        }
        /// <summary>
        /// 根据销售类型历史ID来获取对应的所有的帐龄明细
        /// </summary>
        /// <param name="FTradeSalesId"></param>
        /// <returns></returns>
        public static IDataReader GetAllByFTradeSalesId(int FTradeSalesId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeAgeDetail_GetAllByFTradeSalesId", FTradeSalesId);
            return DB.ExecuteReader(dbCommand);
        }
        /// <summary>
        /// 根据ID　获取应收账款帐龄明细
        /// </summary>
        /// <param name="FTradeAgeId"></param>
        /// <returns></returns>
        public static IDataReader GetByFTradeAgeId(int FTradeAgeId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeAgeDetail_GetByFTradeAgeId", FTradeAgeId);
            return DB.ExecuteReader(dbCommand);
        }


        /// <summary>
        /// 获取可冲帐的记录
        /// </summary>
        /// <param name="WhereSql"></param>
        /// <returns></returns>
        public static DataTable GetBalancePagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeAgeDetail_GetBalancePagination");
            DB.AddInParameter(dbCommand, "WhereSql", DbType.String, WhereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTable dt = DB.ExecuteDataSet(dbCommand).Tables[0];

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }

        /// <summary>
        /// 根据ID　获取最后一条帐龄信息
        /// </summary>
        /// <param name="FTradeID"></param>
        /// <returns></returns>
        public static IDataReader GetLastByFTradeID(int FTradeID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeAgeDetail_GetLastByFTradeID", FTradeID);
            return DB.ExecuteReader(dbCommand);
        }
        /// <summary>
        /// 获取 每个应收款的 付款状态，实时
        /// </summary>
        /// <param name="FTradeAgeId"></param>
        /// <returns></returns>
        public static string GetFKState(int FTradeAgeId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("FTradeAgeDetail_GetFKState", FTradeAgeId);
            return DB.ExecuteScalar(dbCommand).ToString();
        }
    }
}
